<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="两新名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入两新名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          @click="handleExport"
          icon="el-icon-download"
          size="mini"
          type="warning"
        >导出
        </el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="organizationList" @selection-change="handleSelectionChange">
      <el-table-column align="center" type="selection" width="55"/>
      <el-table-column align="center" label="两新名称" prop="name"/>
      <el-table-column align="center" label="所属楼宇" prop="buildingName"/>
      <el-table-column align="center" label="楼层" prop="floor"/>
      <el-table-column align="center" label="浏览量" prop="browseNum"/>
      <el-table-column align="center" label="收藏量" prop="collectNum"/>
      <el-table-column align="center" label="党员数量" prop="partyMemberNum"/>
      <el-table-column align="center" label="党支部名称" prop="partyBranchName"/>
      <el-table-column align="center" label="所属联合党委" prop="partyCommittee"/>
      <el-table-column align="center" label="地址" prop="address"/>
      <el-table-column align="center" label="电话" prop="phone"/>
      <el-table-column align="center" label="上传时间" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改两新组织对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body width="800px">
      <el-form :model="form" :rules="rules" label-width="110px" ref="form">
        <el-row>
          <el-col :span="24">
            <el-form-item label="两新名称" prop="name">
              <el-input placeholder="请输入两新名称" v-model="form.name"/>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="所属楼宇" prop="buildingName">
              <el-select placeholder="请选择" v-model="form.buildingName">
                <el-option
                  :key="bd.buildingId"
                  :label="bd.buildingName"
                  :value="bd.buildingName"
                  v-for="bd in buildingList"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="楼层" prop="floor">
              <el-input placeholder="请输入楼层" v-model="form.floor"/>
            </el-form-item>
          </el-col>

          <el-col :span="24">
            <el-form-item label="业务" prop="business">
              <el-input placeholder="请输入业务" v-model="form.business"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="封面" prop="cover">
              <el-upload
                :action="action"
                :on-success="uploadSuccess"
                :show-file-list="false"
                class="avatar-uploader"
              >
                <img :src="form.cover" class="avatar" v-if="form.cover">
                <i class="el-icon-plus avatar-uploader-icon" v-else></i>
              </el-upload>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="Logo" prop="logo">
              <el-upload
                :action="action"
                :on-success="uploadSuccess2"
                :show-file-list="false"
                class="avatar-uploader"
              >
                <img :src="form.logo" class="avatar" v-if="form.logo">
                <i class="el-icon-plus avatar-uploader-icon" v-else></i>
              </el-upload>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="党员数量" prop="partyMemberNum">
              <el-input placeholder="请输入党员数量" v-model="form.partyMemberNum"/>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="党支部名称" prop="partyBranchName">
              <el-input placeholder="请输入党支部名称" v-model="form.partyBranchName"/>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="所属联合党委" prop="partyCommittee">
              <el-input placeholder="请输入所属联合党委" v-model="form.partyCommittee"/>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="地址" prop="address">
              <el-input placeholder="请输入地址" v-model="form.address"/>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="电话" prop="phone">
              <el-input placeholder="请输入电话" v-model="form.phone"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-form-item label="风采图">
            <el-upload
              :action="action"
              :file-list="fileList"
              :limit="limit"
              :multiple="multipleImg"
              :on-exceed="exceed"
              :on-remove="handleRemove"
              :on-success="uploadSuccess1"
              class="avatar-uploader"
              list-type="picture-card"
            >
              <i class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
            <el-dialog :visible.sync="dialogVisible">
              <img :src="dialogImageUrl" alt="" width="100%">
            </el-dialog>
          </el-form-item>
          <el-col :span="24">
            <el-form-item label="简介" prop="intro">
              <Editor v-model="form.intro"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div class="dialog-footer" slot="footer" style="padding-top:20px">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listBuilding,
    listOrganization,
    getOrganization,
    delOrganization,
    addOrganization,
    updateOrganization,
    exportOrganization
  } from "@/api/system/organization";
  import Editor from '@/components/Editor'

  export default {
    name: "Organization",
    components: {
      Editor
    },
    data() {
      return {
        // 图片上传
        limit: 6,
        multipleImg: true,
        dialogImageUrl: '',
        dialogVisible: false,
        disabled: false,
        imgList: [],
        fileList: [],

        action: process.env.VUE_APP_BASE_API + '/system/upload/file',
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 总条数
        total: 0,
        // 两新组织表格数据
        organizationList: [],
        buildingList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          name: undefined,
          business: undefined,
          browseNum: undefined,
          collectNum: undefined,
          partyMemberNum: undefined,
          partyBranchName: undefined,
          partyCommittee: undefined,
          address: undefined,
          phone: undefined,
          intro: undefined
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          name: [
            {required: true, message: "两新名称不能为空", trigger: "blur"}
          ],
          buildingName: [
            {required: true, message: "楼宇不能为空", trigger: "blur"}
          ],
          business: [
            {required: true, message: "业务不能为空", trigger: "blur"}
          ],
          browseNum: [
            {required: true, message: "浏览量不能为空", trigger: "blur"}
          ],
          collectNum: [
            {required: true, message: "收藏量不能为空", trigger: "blur"}
          ],
          partyMemberNum: [
            {required: true, message: "党员数量不能为空", trigger: "blur"}
          ],
          partyBranchName: [
            {required: true, message: "党支部名称不能为空", trigger: "blur"}
          ],
          partyCommittee: [
            {required: true, message: "所属联合党委不能为空", trigger: "blur"}
          ],
          address: [
            {required: true, message: "地址不能为空", trigger: "blur"}
          ],
          phone: [
            {required: true, message: "电话不能为空", trigger: "blur"}
          ],
          floor: [
            {required: true, message: "电楼层不能为空", trigger: "blur"}
          ],
          createTime: [
            {required: true, message: "创建时间不能为空", trigger: "blur"}
          ],
        }
      };
    },
    created() {
      this.getList();
      this.getBuildingList();
    },

    methods: {
      // 图片上传
      exceed() {
        this.msgInfo(`最多上传${this.limit}张图片`);
      },

      handleRemove(file, fileList) {
        this.fileList = fileList.map(res => {
          if (res.raw) {
            return {
              name: res.name,
              id: res.uid,
              status: res.status,
              raw: res.raw,
              url: res.url
            }
          } else {
            return {
              name: res.name,
              id: res.id,
              status: res.status,
              url: res.url
            }
          }
        })
        console.log('handleRemove', file, fileList, this.fileList);
        this.imgList = []
        this.fileList.map(res => {
          this.imgList.push(res.url)
        })
      },

      handlePictureCardPreview(file) {
        this.dialogImageUrl = file.url;
        this.dialogVisible = true;
      },

      uploadSuccess1(e, file, fileList) {
        console.log('uploadSuccess', e, file, fileList)
        this.fileList = fileList.map(res => {
          if (res.raw) {
            return {
              name: res.name,
              id: res.uid,
              status: res.status,
              raw: res.raw,
              url: res.url,
              response: res.response.code ? res.response.data.path : res.response
            }
          } else {
            return {
              name: res.name,
              id: res.id,
              status: res.status,
              url: res.url
            }
          }
        })
        this.imgList = []
        this.fileList.map(res => {
          this.imgList.push(res.response)
        })
        console.log('fileList', this.fileList, this.imgList)
      },

      uploadSuccess(e) {
        console.log(e)
        if (e.code === 200) {
          this.form.cover = e.data.path
        }
      },

      uploadSuccess2(e) {
        console.log(e)
        if (e.code === 200) {
          this.form.logo = e.data.path
        }
      },

      getBuildingList() {
        listBuilding().then(response => {
          this.buildingList = response.data;
        });
      },
      /** 查询两新组织列表 */
      getList() {
        this.loading = true;
        this.imgList = [];
        listOrganization(this.queryParams).then(response => {
          this.organizationList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: undefined,
          name: undefined,
          business: undefined,
          browseNum: undefined,
          collectNum: undefined,
          partyMemberNum: undefined,
          partyBranchName: undefined,
          partyCommittee: undefined,
          address: undefined,
          phone: undefined,
          createTime: undefined,
          delFlag: undefined,
          intro: undefined,
          buildingId: undefined,
          buildingName: undefined,
          floor: undefined,
          imgs: undefined,
          logo: undefined
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length != 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加两新组织";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getOrganization(id).then(response => {
          this.form = response.data;
          console.log(this.form.logo)
          this.open = true;
          this.title = "修改两新组织";
          if (this.form.imgs) {
            this.imgList = this.form.imgs.split(",")
            // console.log(this.form.imgs, 'imgList', this.imgList)
            this.fileList = this.imgList.map(res => {
              return {
                url: res
              }
            })
          }
        });
      },
      /** 提交按钮 */
      submitForm: function () {
        this.$refs["form"].validate(valid => {
          this.form.imgs = this.imgList.join(',')
          this.fileList = []
          // console.log('submit cover', this.form.imgs, this.imgList)
          if (valid) {
            if (this.form.id != undefined) {
              // console.log(this.form)
              updateOrganization(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("修改成功");
                  this.open = false;
                  this.getList();
                }
              });
            } else {
              addOrganization(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("新增成功");
                  this.open = false;
                  this.getList();
                }
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$confirm('是否确认删除两新组织编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return delOrganization(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function () {
        });
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有两新组织数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return exportOrganization(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function () {
        });
      }
    }
  };
</script>

<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }

  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }

  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }

  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
